package com.iuie.ns.datum.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.iuie.ns.datum.service.IDevProjectService;
import com.iuie.ns.datum.service.IDevTransferService;
import com.iuie.ns.datum.service.ITableFieldService;
import com.iuie.ns.datum.service.ITableStructureService;
import com.iuie.ns.datum.vo.DevProjectVO;
import com.iuie.ns.datum.vo.DevTransferResultVO;
import com.iuie.ns.datum.vo.TableFieldVO;
import com.iuie.ns.datum.vo.TableStructureVO;
import com.iuie.ns.devtools.to.DataSetDevHeaderTO;
import com.iuie.ns.devtools.to.DevHeaderTO;
import com.iuie.ns.devtools.to.JavaBeanDevHeaderTO;
import com.iuie.ns.devtools.to.ResultMapDevHeaderTO;
import com.iuie.ns.devtools.to.SQLDevHeaderTO;
import com.iuie.ns.devtools.transfer.IDevTransfer;
import com.iuie.ns.frame.utils.StringUtils;

@Service("transferService")
public class DevTransferServiceImpl implements IDevTransferService {

	@Resource
	private ITableFieldService tableFieldService;
	
	@Resource
	private ITableStructureService tableStructureService;
	
	@Resource
	private IDevTransfer javaBeanTransfer;
	
	@Resource
	private IDevTransfer resultMapTransfer;
	
	@Resource
	private IDevTransfer javaMapperTransfer;
	
	@Resource
	private IDevTransfer dataSetTransfer;
	
	@Resource
	private IDevTransfer tableCreateMySQLTransfer;
	
	@Resource
	private IDevTransfer tableCreateOracleTransfer;
	
	@Resource
	private IDevProjectService projectService;
	
	
	@Override
	public DevTransferResultVO transToJavaBean(JavaBeanDevHeaderTO header, Long tableId) {
		if (!initDevHeader(header, tableId)) {
			return null;
		}
		List<TableFieldVO> list = tableFieldService.findFieldsByTableId(tableId);
		DevTransferResultVO result = new DevTransferResultVO();
		String s = javaBeanTransfer.transfer(list, header);
		result.setResult(s);
		return result;
	}

	@Override
	public DevTransferResultVO transToResultMap(ResultMapDevHeaderTO header, Long tableId) {
		if (!initDevHeader(header, tableId)) {
			return null;
		}
		// mybatis版本默认用3.0
		List<TableFieldVO> list = tableFieldService.findFieldsByTableId(tableId);
		DevTransferResultVO result = new DevTransferResultVO();
		String s = resultMapTransfer.transfer(list, header);
		result.setResult(s);
		return result;
	}
	
	// 初始化devHeader
	private boolean initDevHeader(DevHeaderTO header, Long tableId) {
		if (tableId == null) {
			return false;
		}
		TableStructureVO table = tableStructureService.findById(tableId);
		if (table == null) {
			return false;
		}
		if (StringUtils.isBlank(header.getClassName())) {
			if (StringUtils.isBlank(table.getClassName())) {
				String enTableName = StringUtils.fieldNameToAttributeName(table.getEnTableName());
				String prefix = StringUtils.upperCase(enTableName.substring(0, 1));
				String suffix = enTableName.substring(1);
				header.setClassName(prefix + suffix + "VO");
			} else {
				header.setClassName(table.getClassName());
			}
		}
		header.setTableName(table.getEnTableName());
		// 获取并设置projectPath
		DevProjectVO project = projectService.findById(table.getProjectId());
		header.setPackagePath(project.getPackagePath());
		if (StringUtils.isBlank(header.getDb()) && StringUtils.isNotBlank(project.getDb())) {
			header.setDb(project.getDb());
		}
		return true;
	}

	@Override
	public DevTransferResultVO transToJavaMapper(ResultMapDevHeaderTO header, Long tableId) {
		if (!initDevHeader(header, tableId)) {
			return null;
		}
		List<TableFieldVO> list = tableFieldService.findFieldsByTableId(tableId);
		DevTransferResultVO result = new DevTransferResultVO();
		String s = javaMapperTransfer.transfer(list, header);
		result.setResult(s);
		return result;
	}

	@Override
	public DevTransferResultVO transToDataSet(DataSetDevHeaderTO header, Long tableId) {
		if (!initDevHeader(header, tableId)) {
			return null;
		}
		List<TableFieldVO> list = tableFieldService.findFieldsByTableId(tableId);
		DevTransferResultVO result = new DevTransferResultVO();
		String s = dataSetTransfer.transfer(list, header);
		result.setResult(s);
		return result;
	}

	@Override
	public DevTransferResultVO transToTableCreateSql(SQLDevHeaderTO header, Long tableId) {
		if (!initDevHeader(header, tableId)) {
			return null;
		}
		List<TableFieldVO> list = tableFieldService.findFieldsByTableId(tableId);
		DevTransferResultVO result = new DevTransferResultVO();
		
		String s = null;
		if (StringUtils.equals(header.getDb(), "mysql")) {
			s = tableCreateMySQLTransfer.transfer(list, header);
		}
		if (StringUtils.equals(header.getDb(), "oracle")) {
			s = tableCreateOracleTransfer.transfer(list, header);
		}
		result.setResult(s);
		return result;
	}

}
